<html>
<head>
<style type="text/css">
	body {
		height: 100%;
	}
	.aim {
		background-image:url(target.png)
		background-repeat: no-repeat;
    	background-position:center center;
		width: 100px;
   		height: 100px;
	}
	
</style>

<title>Argon Alpha Alpha</title>
        <link rel="stylesheet" href="http://pangbo.org/argon/jslog/jslog.css" />
        <link rel="apple-touch-icon" href="assets/sample_icon_57x57.png" />
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
        <meta name="arenabled" />
        
</head>
<body>
	<!--// include the script //-->
	<script src="http://pangbo.org/argon/jslog/jslog.js"></script>
    <script src="https://raw.github.com/mrdoob/three.js/master/build/three.js"></script>	
    <script src="http://argon.gatech.edu/argon_o.js"></script>
	<script src="http://pangbo.org/mobile/models/ColladaLoader.js"></script>
    <!--div class="aim"></div-->	
	<script>
		defenseGame ={
			geoObject:null,
			enemies:null,
			model:null,
			cube:null,
			loaded:null,
			totleEnemy:null,
			enemiesDir:null,
			enemiesPos:null,
			scene:null,
			camera:null,
			renderer:null,
			dirLine:null,
						
			initialization:function(){
				var width = 320;
				var height = 568;
				
				scene = new THREE.Scene();
				camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 750);
				camera.position.x = 0;
				camera.position.y = 0;
				camera.position.z = 0;
				camera.lookAt(new THREE.Vector3(0,0,-1));
				scene.add(camera);
				
				var light = new THREE.PointLight(0xffffff);
				light.position.set(0,0,0);
				scene.add(light);
				
				//for(var i=0; i< totleEnemy; i++){
					//scene.add(enemies[i]);
				//}
				
				var geometry = new THREE.PlaneGeometry( 100, 100 );
	
				var texture = new THREE.ImageUtils.loadTexture( 'target.png' );
				var material = new THREE.MeshBasicMaterial( { map: texture } );
				var aim = new THREE.Mesh( geometry, material );
				aim.position.set(0, 0, -500);
				scene.add( aim );	
				
			/*	
				var img = new THREE.MeshBasicMaterial({ //CHANGED to MeshBasicMaterial
					map:THREE.ImageUtils.loadTexture('target.png')
				});
				img.map.needsUpdate = true; //ADDED
				*/

				renderer = new THREE.WebGLRenderer();
				renderer.setSize( width, height );
				document.body.appendChild( renderer.domElement );
				//geoObject.add(light);
				//ARGON.World.add( geoObject );
				//ARGON.onRender = defenseGame.update;
	
			},
			loadModels:function(){
				loader = new THREE.ColladaLoader();
				loader.options.convertUpAxis = true;
				totleEnemy = 20;
				enemies = new Array();
				enemiesDir = new Array();
				enemiesPos = new Array();
			
				loader.load( 'http://www.prism.gatech.edu/~xxie37/ar/models/beholder/beholder.dae', function ( collada ) {
					for(var i=0; i< totleEnemy; i++){		
						model = collada.scene.clone();
						model.scale.x = model.scale.y = model.scale.z = 30;	
						fai = Math.random()*6.28;
						sita = Math.random()*3.14*50/180;
						model.rotation.x = Math.random()*3.14;
						model.position.z = 500*Math.cos(sita)*Math.sin(fai);
						model.position.y = 500*Math.sin(sita);
						model.position.x = 500*Math.cos(sita)*Math.cos(fai);
						//model.position.x = 500-i*100;
						dir = new Object();
						pos = new Object();
						dir.phi = (Math.random()-0.5)/50;
						dir.theta = (Math.random()-0.5)/50;
						enemiesDir[i] = dir;
						pos.phi = fai;
						pos.theta = sita;
						enemiesPos[i] = pos;
						enemies[i] = model;
						
					}
					defenseGame.initialization();
					
					
					defenseGame.createAxis(defenseGame.vertex(-1000,0,0),defenseGame.vertex(1000,0,0),0xFF0000);
					defenseGame.createAxis(defenseGame.vertex(0,-1000,0),defenseGame.vertex(0,1000,0),0x00FF00);
					defenseGame.createAxis(defenseGame.vertex(0,0,-1000),defenseGame.vertex(0,0,1000),0x0000FF);
					
					var lineGeometry = new THREE.Geometry();
					var lineMat = new THREE.LineBasicMaterial({color: 0xFFFF00, lineWidth: 10});
					lineGeometry.vertices.push(defenseGame.vertex(0,0,500),defenseGame.vertex(0,0,0));
					dirLine = new THREE.Line(lineGeometry, lineMat);
					scene.add(dirLine);
					
					defenseGame.draw();
				});				
			},
			vertex:function(x,y,z){ 
           		return new THREE.Vector3(x,y,z); 
    		},
			setGeoObject:function(){				
			},
			createAxis:function(p1, p2, color){
				var line, lineGeometry = new THREE.Geometry(),
				lineMat = new THREE.LineBasicMaterial({color: color, lineWidth: 1});
				lineGeometry.vertices.push(p1, p2);
				line = new THREE.Line(lineGeometry, lineMat);
				scene.add(line);
   			},
			draw:function(){
				requestAnimationFrame(defenseGame.draw);
				defenseGame.update();
				renderer.render(scene, camera);				
			},
			update:function(){
				
				var da = ARGON.deviceAttitude;
				var str = da[0] + " " + da[1] + " " + da[2] + " " + da[3] + "\n "
				+da[4] + " " + da[5] + " " + da[6] + " " + da[7] + "\n "
				+da[8] + " " + da[9] + " " + da[10] + " " + da[11] + "\n "
				+da[12] + " " + da[13] + " " + da[14] + " " + da[15] + "\n ";
				var matrix = new THREE.Matrix4(da[0],da[1],da[2],da[3],da[4],da[5],da[6],da[7],da[8],da[9],da[10],da[11],da[12],da[13],da[14],da[15]);
				var rotation = matrix.decompose()[1];
				
				//x: direct to north, y: direct to z: direct to up
				//console.log("x: "+(-rotation.x.toFixed(2))+"y: "+(-rotation.y.toFixed(2))+"z: "+(-rotation.z.toFixed(2)))
				rotation.normalize()
				
				var qw = rotation.w;
				var angle = 2*Math.acos(qw)*57;
				var qx = rotation.x;
				var qy = rotation.y;
				var qz = rotation.z;
				var s = Math.sqrt(1-qw*qw);
				var x,y,z;
				if(s<0.0001){
					x = qx;
					y = qy;
					z = qz;
				}
				else{
					x = qx/s;
					y = qy/s;
					z = qz/s;
				}
				
				//console.log("x: "+x.toFixed(2)+" y: "+y.toFixed(2)+" z: "+z.toFixed(2)+" angle: "+angle.toFixed(2));
				//dirLine.lookAt(new THREE.Vector3(enemies[0].position.x,enemies[0].position.y,enemies[0].position.z).normalize());
				//dirLine.lookAt(new THREE.Vector3(x,y,z).normalize());
				//camera.lookAt(new THREE.Vector3(-qx,-qy,-qz).normalize());
				//defenseGame.cube.position.x +=10;
				
				/*
				heading = Math.atan2(2*qy*qw-2*qx*qz,1-2*qy*qy-2*qz*qz);
				attitude = Math.asin(2*qx*qy + 2*qz*qw);
				bank = Math.atan2(2*qx*qw-2*qy*qz , 1 - 2*qx*qx - 2*qz*qz);
				
				*/


				var test = qx*qy + qz*qw;
				if (test > 0.499) { // singularity at north pole
					heading = 2 * Math.atan2(qx,qw);
					attitude = Math.PI/2;
					bank = 0;
				}
				else{
					if (test < -0.499) { // singularity at south pole
						heading = -2 * Math.atan2(qx,qw);
						attitude = - Math.PI/2;
						bank = 0;
					}
					else{
					
						var sqx = qx*qx;
						var sqy = qy*qy;
						var sqz = qz*qz;
						
						heading = Math.atan2(2*qy*qw-2*qx*qz , 1 - 2*sqy - 2*sqz);
						attitude = Math.asin(2*test);
						bank = Math.atan2(2*qx*qw-2*qy*qz , 1 - 2*sqx - 2*sqz)
					}
				}

				//var quaternion = new THREE.Quaternion();
				//quaternion.setFromEuler(new THREE.Vector3(-attitude, -heading, 0));
				//camera.useQuaternion =true;
				//camera.quaternion=quaternion;
				camera.eulerOrder = "YXZ";
				camera.rotation.y = -heading;
				camera.rotation.x = -attitude;
				//camera.rotation.y = -pitch;
				//camera.rotation.x = -roll;
				//var vec1 = new THREE.Vector3(0,0,-1);
				//vec1.rotation.x = -attitude;
				//vec1.rotation.y = -heading;
				//camera.lookAt(vec1);
				
				console.log("heading: "+(heading*57).toFixed(2)+" attitude:  "+(attitude*57).toFixed(2)+" bank: "+(bank*57).toFixed(2));
				
				for(var i=0; i< totleEnemy; i++){
					//console.log(enemiesPos[i].theta);
					if(enemiesPos[i].theta>3.14*50/180 || enemiesPos[i].theta<0)
					{
						enemiesDir[i].theta = -enemiesDir[i].theta;
					}
					else if(Math.random()<0.01){
						enemiesDir[i].phi = (Math.random()-0.5)/50;
						enemiesDir[i].theta = (Math.random()-0.5)/50;
					}
					enemiesPos[i].theta +=enemiesDir[i].theta;
					enemiesPos[i].phi +=enemiesDir[i].phi;
					
					//enemies[i].position.x = 500*Math.cos(enemiesPos[i].theta)*Math.sin(enemiesPos[i].phi);
					//enemies[i].position.y = 500*Math.sin(enemiesPos[i].theta);
					//enemies[i].position.z = 500*Math.cos(enemiesPos[i].theta)*Math.cos(enemiesPos[i].phi);
					//enemies[i].rotation.x+=0.1;
					//enemies[i].rotation.y+=0.1;
				}
				//camera.lookAt(new THREE.Vector3(enemies[0].position.x,enemies[0].position.y,enemies[0].position.z).normalize());
			}
		}
	
	</script>
	
	<script>
		
		
		document.addEventListener("AR.ArgonReadyEvent", defenseGame.loadModels());
		//defenseGame.draw();
			
    </script>
	
	

</body>
</html> 